/*
 * Board.cpp
 *
 *  Created on: 13/06/2014
 *      Author: lucio
 */

#include "Board.h"
#include <iostream>
#define COLUMN_MIN 12
#define ROW_MIN 12
#define PIXELS 33
#define THRASH 15
#define BACKGROUND "BackgroundTile32.png"

Board::Board(Glib::RefPtr<Gtk::Builder>& builder) {
	this->clickedCell = NULL;
	builder->get_widget("Board",this->board);
	builder->get_widget("EventBoard",this->eventBoard);
	this->eventBoard->set_events(Gdk::BUTTON_PRESS_MASK);

	this->RowCount = ROW_MIN;
	this->ColumnCount = COLUMN_MIN;

	ClearBoard();
	AddRows(this->RowCount, this->ColumnCount);
	AddColumns(this->RowCount, this->ColumnCount);
	AddBackground(this->RowCount, this->ColumnCount);
	UpdateCellMatrix(this->RowCount,this->ColumnCount);

	this->eventBoard->signal_button_press_event().connect(sigc::mem_fun
				(*this,&Board::BoardClicked));
}

Board::Board(){

}

void Board::UpdateCellMatrix(int rowCount, int columnCount) {
	cells.resize(rowCount);
	for (int i = 0; i < rowCount; i++) {
		cells[i].resize(columnCount);
	}
	for (int i = 0; i < rowCount; i++) {
		for (int j = 0; j < columnCount; j++) {
			cells[i][j] = new Cell(i,j,BACKGROUND);
		}
	}
	this->selectedImage = new Gtk::Image("ExplodableBlock32.png");
	this->board->put(*selectedImage, 100000, 100000);
}

void Board::ClearBoard(){
	std::vector<Gtk::Widget*> separadores = this->board->get_children();
	std::vector<Gtk::Widget*>::iterator it;
	std::vector<Gtk::Widget*>::iterator begin = separadores.begin();
	std::vector<Gtk::Widget*>::iterator end = separadores.end();
	for(it = begin; it != end; it++){
		this->board->remove(*(*it));
	}
}

void Board::AddColumns(int rowCount, int columnCount) {
	for (int i = 0; i <= rowCount; i++) {
		Gtk::VSeparator* vertSep = new Gtk::VSeparator();
		vertSep->set_size_request(PIXELS, PIXELS* (columnCount+1));
		this->board->put(*vertSep, i*PIXELS, 0);
	}
}

void Board::AddRows(int rowCount, int columnCount) {
	for (int i = 0; i <= rowCount; i++) {
		Gtk::HSeparator* horizSep = new Gtk::HSeparator();
		horizSep->set_size_request(PIXELS * (rowCount+1), PIXELS);
		this->board->put(*horizSep, 0, i * PIXELS);
	}
}

void Board::AddBackground(int rowCount, int columnCount) {
	for (int i = 0; i < rowCount*PIXELS; i=i+PIXELS) {
		for (int j = 0; j < columnCount*PIXELS; j=j+PIXELS) {
			Gtk::Image* image = new Gtk::Image(BACKGROUND);
			int fila = (i)/PIXELS;
			int columna = (j)/PIXELS;
			int centered_x = columna * PIXELS+PIXELS/2;
			int centered_y = fila * PIXELS+PIXELS/2;
			this->board->put(*image,centered_x,centered_y);
		}
	}
	this->board->show_all();
}

bool Board::BoardClicked(GdkEventButton* event){
	int x = 0, y = 0;
	this->board->get_pointer(x,y);
	int row = (y-THRASH)/PIXELS;
	int column = (x-THRASH)/PIXELS;
	if ( x > (ColumnCount *PIXELS+THRASH))
		return false;
	if ( y > (RowCount *PIXELS+THRASH))
		return false;
	this->clickedCell = new Cell(row,column,selectedImage->get_name());
	this->cells[row][column]=clickedCell;
	PutImageInBoard(x,y);
	return true;
}

void Board::PutImageInBoard(int x, int y){
	int fila = (y-THRASH)/PIXELS;
	int columna = (x-THRASH)/PIXELS;
	int centered_x = columna * PIXELS+PIXELS/2;
	int centered_y = fila * PIXELS+PIXELS/2;
	Gtk::Image* image = new Gtk::Image("ExplodableBlock32.png");
	this->board->put(*image,centered_x,centered_y);
	image->show();
}
int Board::getColumnCount() const {
	return ColumnCount;
}

void Board::setColumnCountInc() {
	ColumnCount++;
}
void Board::setColumnCountDec() {
	ColumnCount--;
}

void Board::setRowCountInc() {
	RowCount++;
}

void Board::setRowCountDec() {
	RowCount--;
}

int Board::getRowCount() const {
	return RowCount;
}

Board::~Board() {
	// TODO Auto-generated destructor stub
}

